﻿using BDPAutodetect.Mgrcore;
using BDPAutodetect.Models;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace BDPAutodetect.Client
{
    /// <summary>
    /// 工位设置管理视图模型类
    /// </summary>
    public partial class SettingStationViewModel : BaseViewModel<UserControl>
    {
        /// <summary>
        /// 用户管理实例
        /// </summary>
        protected UserManager UserMgr { get; set; }
        /// <summary>
        /// 工位管理实例
        /// </summary>
        protected StationManager StationMgr { get; set; }
        /// <summary>
        /// 工位数据集
        /// </summary>
        [ObservableProperty]
        protected ObservableCollection<MStation> stationItems;

        /// <summary>
        /// 构造函数
        /// </summary>
        public SettingStationViewModel(UserManager argUserManager, StationManager argStationManager)
        {
            this.UserMgr = argUserManager;
            this.StationMgr = argStationManager;
            this.StationItems = new ObservableCollection<MStation>();
        }

        /// <summary>
        /// 初始化加载权限编辑
        /// </summary>
        /// <returns></returns>
        protected async Task InitLoadPermission()
        {
            if (this.PowerPermission == null) return;
            var resultTaskPowers = this.UserMgr.GetUserPowers(this.PowerPermission.Power.ID);
            await resultTaskPowers.ConfigureAwait(false);
            if (resultTaskPowers != null && resultTaskPowers.Result != null && resultTaskPowers.Result.Count > 0)
            {
                this.IsVisibilityPermission = Visibility.Visible;
            }
        }

        /// <summary>
        /// 初始化加载查询选项数据集
        /// </summary>
        /// <returns></returns>
        protected async Task InitLoadItems()
        {
            await Task.Factory.StartNew(() =>
            {
                //初始化工位数据
                foreach (var stationItem in this.StationMgr.Stations)
                {
                    if (stationItem.ID <= 0) continue;
                    this.StationItems.Add(stationItem);
                }
            }, new CancellationTokenSource().Token, TaskCreationOptions.None, base.syncContextTask);
        }

        /// <summary>
        /// 页面初始化加载事件
        /// </summary>
        [RelayCommand]
        public async Task ExecuteLoaded()
        {
            if (this.IsLoaded) return;

            //获取编辑权限
            var resultTask = this.InitLoadPermission();
            await resultTask.ConfigureAwait(false);
          
            //加载数据
            resultTask = this.InitLoadItems();
            await resultTask.ConfigureAwait(false);

            //标记初始化加载完成
            this.IsLoaded = true;
        }

        /// <summary>
        /// 编辑更新事件
        /// </summary>
        /// <param name="argStation">工位信息</param>
        /// <returns></returns>
        [RelayCommand]
        public async Task ExecuteEditor(MStation argStation)
        {
            SettingEditorDialog editorDialog = new SettingEditorDialog("工位", argStation);
            bool? bResult = editorDialog.ShowDialog();
            if (!bResult.HasValue || !bResult.Value) return;
            if (editorDialog.DialogResultData == null) return;

            MStation mStation = new MStation()
            {
                ID = editorDialog.DialogResultData.ID,
                Code = editorDialog.DialogResultData.Code,
                Name = editorDialog.DialogResultData.Name
            };
            //更新数据库
            var result = this.StationMgr.UpdateStation(mStation);
            await result.ConfigureAwait(false);
        }
    }
}
